//
//  WJStatusViewModel.swift
//  WJWeiBo
//
//  Created by wangjun on 16/7/25.
//  Copyright © 2016年 wangjun. All rights reserved.
//

import UIKit

class WJStatusViewModel: NSObject {

    // 定义一个属性
    var status: WJStatus?
    // 微博认证用户图片
    var avatarImage: UIImage?
    //处理会员等级图片
    var memberImage: UIImage?
    
    // 微博来源处理富文本
    var sourceAttr: NSMutableAttributedString?
    
    // 转发数
    var repostsCountStr: String?
    // 评论数
    var commentsCountStr: String?
    // 表态数
    var attitudesCountStr: String?
    // 微博时间(只读属性 计算型)
    var sinaTimeStr: String?{
        return NSDate.sinaDate(status?.created_at ?? "")?.sinaDescripitionStr
    }
    
    // 重载构造函数
    init(status: WJStatus) {
        super.init()
        self.status = status
        // 在viewModel调用重载构造函数初始化的时候 我们要对avatarImage&memberImage 赋值
        self.avatarImage = getAvatarImage(status.user?.verified)
        self.memberImage = getMemberImage(status.user?.mbrank)
        self.sourceAttr = getSource(status.source)
        self.repostsCountStr = getSinaCount(status.reposts_count, title: "转发")
        self.commentsCountStr = getSinaCount(status.comments_count, title: "评论")
        self.attitudesCountStr = getSinaCount(status.attitudes_count, title: "赞")
    }
}
extension WJStatusViewModel {
    
    func getSinaCount(count: Int,title: String)-> String {
        
        if count <= 0 {
            
            return title
        }else {
            
            if count < 10000 {
                
                return "\(count)"
            }else{
                
                let sinaCount = CGFloat(count/1000)/10
                var sinaCountStr = "\(sinaCount)万"
                if sinaCountStr.containsString(".0") {
                    sinaCountStr = sinaCountStr.stringByReplacingOccurrencesOfString(".0", withString: "")
                }
                return sinaCountStr
                
            }
        }
        
    }
    
}




// MARK: 数据处理
extension WJStatusViewModel {
    // 处理微博认证用户图片/// 认证类型 -1：没有认证，1，认证用户，2,3,5: 企业认证，220: 达人
    func getAvatarImage(verified: Int?) -> UIImage?{
        // 判读他是否为nil
        guard let ver = verified else{
            return nil
        }
        
        switch ver {
        case 1:
            return UIImage(named: "avatar_vip")
        case 2,3,5:
            return UIImage(named: "avatar_enterprise_vip")
        case 220:
            return UIImage(named: "avatar_grassroot")
        default:
            return UIImage(named: "avatar_vgirl")
        }
        
    }
    
    // 处理会员等级图片/// 会员等级 1-6
    func getMemberImage(mbrank: Int?) -> UIImage?{
        // 判断他是否为nil
        guard let mbr = mbrank else {
            return nil
        }
        if mbr > 0 && mbr < 7 {
            return UIImage(named: "common_icon_membership_level\(mbr)")
        }
        return UIImage(named: "common_icon_membership")
    }
}
// 当前认为是MVVM开发模式
extension WJStatusViewModel{
    
    func getSource(source:String?)-> NSMutableAttributedString?{
        guard let p = source  where p.containsString("\">") else{
            
            return nil
        }
        //起始位置
        let startRange = p.rangeOfString("\">")!
        //结束位置
        let endRange = p.rangeOfString("</")!
        //字符串的截取
        let result = p.substringWithRange(startRange.endIndex..<endRange.startIndex)
        
        return getSourceAttr(result)
        
    }
    
    func getSourceAttr(result: String)-> NSMutableAttributedString {
        let name = "来自 " + result
        let range = (name as NSString).rangeOfString("来自")
        let attr = NSMutableAttributedString(string: name)
        attr.addAttributes([NSForegroundColorAttributeName: UIColor.grayColor()], range: range)
        
        return attr
    }
    
}



